package Methods;

import java.util.ArrayList;

import function.*;

public class SlopeMethods {
	public static ArrayList<ArrayList<Double>> Newton_Raphson(Function f,
			double p0) {
		double pi = p0;
		int counter = 0;
		Function df = f.differentiate();
		System.out.println(df.toString());
		ArrayList<ArrayList<Double>> out = new ArrayList<ArrayList<Double>>();
		while (true) {
			double fpi = f.evaluate(pi);
			if (Math.abs(fpi) < 0.00000001)
				break;
			double dfpi = df.evaluate(pi);
			if (Math.abs(dfpi) < 0.00000001) {
				System.out.println("Derivative of the function" + f.toString()
						+ " is about zero when x = " + pi);
				break;
			}
			System.out.println(pi + " " + fpi);
			pi -= (double) (fpi / dfpi);
			out.add(new ArrayList<Double>());
			out.get(counter).add(pi);
			out.get(counter).add(fpi);
			counter++;
		}
		return out;
	}
}
